<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="Android," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.1.2" />






<meta name="description" content="MVP大家最先想到的应该是LOL和CF里面的MVP荣誉吧，玩过的应该都知道指的是Most-Valuable-Player(全场表现最佳DE游戏玩家) 以前也经常玩，平常凌晨一两点，周末凌晨三四点，想想那段时间还是挺疯的，浪费了那么多时间，现在得赶紧抓紧时间好好学习，多写简书，好好工作，定个小目标，挣他一个亿！ 好了废话就不多说，进入正题，我们这里说的MVP则是一种设计模式，说起MVP那就要先说说M">
<meta name="keywords" content="Android">
<meta property="og:type" content="article">
<meta property="og:title" content="你应该学会的设计模式MVP">
<meta property="og:url" content="http://soaic.github.io/2016/09/05/2016-09-06-mvp/index.html">
<meta property="og:site_name" content="Soaic&#39;s Blog">
<meta property="og:description" content="MVP大家最先想到的应该是LOL和CF里面的MVP荣誉吧，玩过的应该都知道指的是Most-Valuable-Player(全场表现最佳DE游戏玩家) 以前也经常玩，平常凌晨一两点，周末凌晨三四点，想想那段时间还是挺疯的，浪费了那么多时间，现在得赶紧抓紧时间好好学习，多写简书，好好工作，定个小目标，挣他一个亿！ 好了废话就不多说，进入正题，我们这里说的MVP则是一种设计模式，说起MVP那就要先说说M">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1460246-d0e51309dff6a8f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1460246-ea98d849ecd73be5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1460246-cadba6a41a9a8738.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1460246-565de74e7178c7c3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2017-08-17T17:45:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="你应该学会的设计模式MVP">
<meta name="twitter:description" content="MVP大家最先想到的应该是LOL和CF里面的MVP荣誉吧，玩过的应该都知道指的是Most-Valuable-Player(全场表现最佳DE游戏玩家) 以前也经常玩，平常凌晨一两点，周末凌晨三四点，想想那段时间还是挺疯的，浪费了那么多时间，现在得赶紧抓紧时间好好学习，多写简书，好好工作，定个小目标，挣他一个亿！ 好了废话就不多说，进入正题，我们这里说的MVP则是一种设计模式，说起MVP那就要先说说M">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/1460246-d0e51309dff6a8f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post","offset":12,"offset_float":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://soaic.github.io/2016/09/05/2016-09-06-mvp/"/>





  <title>你应该学会的设计模式MVP | Soaic's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Soaic's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">Soaic</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-music">
          <a href="/music/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-music"></i> <br />
            
            音乐
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://soaic.github.io/2016/09/05/2016-09-06-mvp/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Soaic">
      <meta itemprop="description" content="Welcome!">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Soaic's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">你应该学会的设计模式MVP</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-09-05T22:04:00+08:00">
                2016-09-05
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android框架/" itemprop="url" rel="index">
                    <span itemprop="name">Android框架</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          
             <span id="/2016/09/05/2016-09-06-mvp/" class="leancloud_visitors" data-flag-title="你应该学会的设计模式MVP">
               <span class="post-meta-divider">|</span>
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               
                 <span class="post-meta-item-text">阅读次数 </span>
               
                 <span class="leancloud-visitors-count"></span>
             </span>
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>MVP大家最先想到的应该是LOL和CF里面的MVP荣誉吧，玩过的应该都知道指的是Most-Valuable-Player(全场表现最佳DE游戏玩家)</p>
<p>以前也经常玩，平常凌晨一两点，周末凌晨三四点，想想那段时间还是挺疯的，浪费了那么多时间，现在得赶紧抓紧时间好好学习，多写简书，好好工作，定个小目标，挣他一个亿！</p>
<p>好了废话就不多说，进入正题，我们这里说的MVP则是一种设计模式，说起MVP那就要先说说MVC设计模式了，MVP就是由MVC演变而来的。</p>
<blockquote>
<p>MVC由Model、View、Control组成。<br>Model数据模型，提供数据<br>View视图模型，提供视图展示<br>Control控制器，负责控制Model和View通信</p>
</blockquote>
<a id="more"></a>
<p>MVC在Android中的应用，如下图，Activity为Control，XML为View，请求网络数据模块为Model。View箭头指向Control意为传递数据，那就是Control获取View的数据传递给Model来请求网络，请求到的数据直接传递给View来显示，这是一条主线。还有一条就是底部这两个箭头，也就是说Model可以不通过Control，直接获取View的数据，然后再返回结果数据给View。</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1460246-d0e51309dff6a8f6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="MVC.png"></p>
<blockquote>
<p>MVP由Model、View、Presenter组成。<br>Model数据模型，提供数据<br>View视图模型，提供视图展示<br>Presenter主持者，负责逻辑处理</p>
</blockquote>
<p>MVP与MVC最明显的区别就在于Presenter和Control了。如下图，Presenter传递数据给Model，Model得到数据来请求网络，再返回数据给Presenter，Presenter再把得到的Model数据传递给View显示。MVP整体的一个流程就是这样。相比MVC来说，Model和View互相不干涉，达到完全解耦</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1460246-ea98d849ecd73be5.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="MVP.png"></p>
<p>估计上面文字性的解释都听得有点晕，那我们就拿个例子来实战一下吧</p>
<blockquote>
<p>下面和大家一起完成一个简单查询快递信息的例子，里面用到了当下最流行的架构RxJava+Retrofit+MVP+OkHttp，非常值得大家一学</p>
</blockquote>
<h3 id="配置环境"><a href="#配置环境" class="headerlink" title="配置环境"></a>配置环境</h3><p>在builde.gradle里面添加  </p>
<figure class="highlight javascript"><table><tr><td class="code"><pre><div class="line">compile <span class="string">'com.android.support:appcompat-v7:23.3.0'</span></div><div class="line">compile <span class="string">'com.squareup.retrofit2:retrofit:2.1.0'</span></div><div class="line">compile <span class="string">'com.squareup.retrofit2:adapter-rxjava:2.1.0'</span></div><div class="line">compile <span class="string">'com.squareup.retrofit2:converter-gson:2.1.0'</span></div><div class="line">compile <span class="string">'com.squareup.okhttp3:okhttp:3.4.1'</span></div><div class="line">compile <span class="string">'io.reactivex:rxandroid:1.2.1'</span></div><div class="line">compile <span class="string">'io.reactivex:rxjava:1.1.6'</span></div><div class="line">compile <span class="string">'com.squareup.okhttp3:logging-interceptor:3.4.1'</span></div></pre></td></tr></table></figure>
<p>在AndroidManifest.xml添加所需权限</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.INTERNET"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;<span class="name">uses-permission</span> <span class="attr">android:name</span>=<span class="string">"android.permission.WRITE_EXTERNAL_STORAGE"</span>/&gt;</span></div></pre></td></tr></table></figure>
<h3 id="初始化配置Retrofit"><a href="#初始化配置Retrofit" class="headerlink" title="初始化配置Retrofit"></a>初始化配置Retrofit</h3><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainApp</span> <span class="keyword">extends</span> <span class="title">Application</span> </span>&#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate();</div><div class="line">        <span class="comment">//初始化retrofitUtils</span></div><div class="line">        RetrofitUtils.getInstance().initOkHttp(<span class="keyword">this</span>);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>在MainApp中初始化Retrofit,设置超时和baseUrl，添加了日志拦截以及RxJava和数据解析，并暴露一个getRetrofit()方法供需要的地方调用<br><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RetrofitUtils</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> Retrofit retrofit;</div><div class="line">    <span class="keyword">private</span> String baseUrl = <span class="string">"http://www.kuaidi100.com/"</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">SingleLoader</span></span>&#123;</div><div class="line">        <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> RetrofitUtils INSTANCE = <span class="keyword">new</span> RetrofitUtils();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> RetrofitUtils <span class="title">getInstance</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">return</span> SingleLoader.INSTANCE;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">initOkHttp</span><span class="params">(@NonNull Context context)</span></span>&#123;</div><div class="line">        HttpLoggingInterceptor interceptor = <span class="keyword">new</span> HttpLoggingInterceptor();</div><div class="line">        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);</div><div class="line">        OkHttpClient client = <span class="keyword">new</span> OkHttpClient.Builder()</div><div class="line">                .connectTimeout(<span class="number">10000L</span>,TimeUnit.MILLISECONDS)       <span class="comment">//设置连接超时</span></div><div class="line">                .readTimeout(<span class="number">10000L</span>,TimeUnit.MILLISECONDS)          <span class="comment">//设置读取超时</span></div><div class="line">                .writeTimeout(<span class="number">10000L</span>, TimeUnit.MILLISECONDS)         <span class="comment">//设置写入超时</span></div><div class="line">                .cache(<span class="keyword">new</span> Cache(context.getCacheDir(),<span class="number">10</span> * <span class="number">1024</span> * <span class="number">1024</span>))   <span class="comment">//设置缓存目录和10M缓存</span></div><div class="line">                .addInterceptor(interceptor)	<span class="comment">//添加日志拦截器（该方法也可以设置公共参数，头信息）</span></div><div class="line">                .build();</div><div class="line">        retrofit = <span class="keyword">new</span> Retrofit.Builder()</div><div class="line">                .client(client)		<span class="comment">//设置OkHttp</span></div><div class="line">                .baseUrl(baseUrl)</div><div class="line">                .addConverterFactory(GsonConverterFactory.create()) <span class="comment">//  添加数据解析ConverterFactory</span></div><div class="line">                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())   <span class="comment">//添加RxJava</span></div><div class="line">                .build();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> Retrofit <span class="title">getRetrofit</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">return</span> retrofit;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h3 id="Model"><a href="#Model" class="headerlink" title="Model"></a>Model</h3><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">PostSearchModel</span> </span>&#123;</div><div class="line"><span class="comment">/**</span></div><div class="line"><span class="comment"> * 请求快递信息</span></div><div class="line"><span class="comment"> * <span class="doctag">@param</span> type 快递类型</span></div><div class="line"><span class="comment"> * <span class="doctag">@param</span> postid 快递单号</span></div><div class="line"><span class="comment"> * <span class="doctag">@param</span> callback 结果回调</span></div><div class="line"><span class="comment"> */</span></div><div class="line"><span class="function"><span class="keyword">void</span> <span class="title">requestPostSearch</span><span class="params">(String type,String postid,PostSearchCallback callback)</span></span>;</div><div class="line"><span class="class"><span class="keyword">interface</span> <span class="title">PostSearchCallback</span></span>&#123;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">requestPostSearchSuccess</span><span class="params">(PostQueryInfo postQueryInfo)</span></span>;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">requestPostSearchFail</span><span class="params">(String failStr)</span></span>;</div><div class="line">	&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>创建这个PostSearchModel接口，方便后期可以不修改之前代码灵活切换多种实现方式</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">PostServiceBiz</span> </span>&#123;</div><div class="line">    <span class="meta">@POST</span>(<span class="string">"query"</span>)</div><div class="line">    <span class="function">Observable&lt;PostQueryInfo&gt; <span class="title">searchRx</span><span class="params">(@Query(<span class="string">"type"</span>)</span> String type, @<span class="title">Query</span><span class="params">(<span class="string">"postid"</span>)</span> String postid)</span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostQueryInfo</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> String message;</div><div class="line">    <span class="keyword">private</span> String nu;</div><div class="line">    <span class="keyword">private</span> String ischeck;</div><div class="line">    <span class="keyword">private</span> String com;</div><div class="line">    <span class="keyword">private</span> String status;</div><div class="line">    <span class="keyword">private</span> String condition;</div><div class="line">    <span class="keyword">private</span> String state;</div><div class="line">    <span class="keyword">private</span> List&lt;DataBean&gt; data;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">DataBean</span> </span>&#123;</div><div class="line">        <span class="keyword">private</span> String time;</div><div class="line">        <span class="keyword">private</span> String context;</div><div class="line">        <span class="keyword">private</span> String ftime;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>PostQueryInfo没有添加get和set方法，大家自行快捷键了<br>PostSearchModelImpl实现上面PostSearchModel接口，利用上一篇文章里学的Retrofit结合RxJava来请求网络<br>这里传了PostSearchCallback回调函数，成功或者失败都通过回调函数返回，完全不需要考虑其它</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostSearchModelImpl</span> <span class="keyword">implements</span> <span class="title">PostSearchModel</span> </span>&#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">requestPostSearch</span><span class="params">(String type, String postid, <span class="keyword">final</span> PostSearchCallback callback)</span> </span>&#123;</div><div class="line">        RetrofitUtils.getInstance()</div><div class="line">                .getRetrofit()</div><div class="line">                .create(PostServiceBiz.class)</div><div class="line">                .searchRx(type,postid)</div><div class="line">                <span class="comment">//访问网络切换异步线程</span></div><div class="line">                .subscribeOn(Schedulers.io())</div><div class="line">                <span class="comment">//响应结果处理切换成主线程</span></div><div class="line">                .observeOn(AndroidSchedulers.mainThread())</div><div class="line">                .subscribe(<span class="keyword">new</span> Subscriber&lt;PostQueryInfo&gt;() &#123;</div><div class="line">                    <span class="meta">@Override</span></div><div class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCompleted</span><span class="params">()</span> </span>&#123;</div><div class="line">                        <span class="comment">//请求结束回调</span></div><div class="line">                    &#125;</div><div class="line">                    <span class="meta">@Override</span></div><div class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</div><div class="line">                        <span class="comment">//错误回调</span></div><div class="line">                        callback.requestPostSearchFail(e.getMessage());</div><div class="line">                    &#125;</div><div class="line">                    <span class="meta">@Override</span></div><div class="line">                    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onNext</span><span class="params">(PostQueryInfo postQueryInfo)</span> </span>&#123;</div><div class="line">                        <span class="comment">//成功结果返回</span></div><div class="line">                        callback.requestPostSearchSuccess(postQueryInfo);</div><div class="line">                    &#125;</div><div class="line">                &#125;);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="View"><a href="#View" class="headerlink" title="View"></a>View</h3><p>activity_main.xml主界面布局</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">LinearLayout</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></div><div class="line"><span class="tag">    <span class="attr">xmlns:tools</span>=<span class="string">"http://schemas.android.com/tools"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:padding</span>=<span class="string">"16dp"</span></span></div><div class="line"><span class="tag">    <span class="attr">tools:context</span>=<span class="string">".ui.MainActivity"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:orientation</span>=<span class="string">"vertical"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">LinearLayout</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:orientation</span>=<span class="string">"horizontal"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"快递公司名："</span>/&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">EditText</span></span></div><div class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/post_name_et"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"yuantong"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">LinearLayout</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:orientation</span>=<span class="string">"horizontal"</span>&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"快 递  单 号："</span></span></div><div class="line"><span class="tag">            /&gt;</span></div><div class="line">        <span class="tag">&lt;<span class="name">EditText</span></span></div><div class="line"><span class="tag">            <span class="attr">android:id</span>=<span class="string">"@+id/post_id_et"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">            <span class="attr">android:text</span>=<span class="string">"500379523313"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">Button</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/post_search_bn"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">"查询"</span>/&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">ListView</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/post_list_lv"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:listSelector</span>=<span class="string">"@null"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:background</span>=<span class="string">"@null"</span>/&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></div></pre></td></tr></table></figure>
<p>view_item_logistics.xml<br>listview适配器的item布局</p>
<figure class="highlight xml"><table><tr><td class="code"><pre><div class="line">&lt;?xml version="1.0" encoding="utf-8"?&gt;</div><div class="line"><span class="tag">&lt;<span class="name">LinearLayout</span> <span class="attr">xmlns:android</span>=<span class="string">"http://schemas.android.com/apk/res/android"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:layout_width</span>=<span class="string">"match_parent"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:background</span>=<span class="string">"#FFFFFF"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:orientation</span>=<span class="string">"vertical"</span></span></div><div class="line"><span class="tag">    <span class="attr">android:padding</span>=<span class="string">"15dp"</span>&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/tv_conent"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:textColor</span>=<span class="string">"#8f8f8f"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:textSize</span>=<span class="string">"14sp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">"【深圳市】广东分公司已出发"</span> /&gt;</span></div><div class="line">    <span class="tag">&lt;<span class="name">TextView</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_marginTop</span>=<span class="string">"13dp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:id</span>=<span class="string">"@+id/tv_date"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_width</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_height</span>=<span class="string">"wrap_content"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:textColor</span>=<span class="string">"#8f8f8f"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:textSize</span>=<span class="string">"12sp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:layout_marginBottom</span>=<span class="string">"13dp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:paddingLeft</span>=<span class="string">"6dp"</span></span></div><div class="line"><span class="tag">        <span class="attr">android:text</span>=<span class="string">"2016-05-03 18:23"</span> /&gt;</span></div><div class="line"><span class="tag">&lt;/<span class="name">LinearLayout</span>&gt;</span></div></pre></td></tr></table></figure>
<p>ListView适配器<br><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">LogisticsAdapter</span> <span class="keyword">extends</span> <span class="title">BaseAdapter</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> List&lt;PostQueryInfo.DataBean&gt; datas;</div><div class="line">    <span class="keyword">private</span> LayoutInflater inflater;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">LogisticsAdapter</span><span class="params">(Context context, List&lt;PostQueryInfo.DataBean&gt; datas)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.datas = datas;</div><div class="line">        inflater = LayoutInflater.from(context);</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> View <span class="title">getView</span><span class="params">(<span class="keyword">int</span> i, View view, ViewGroup viewGroup)</span> </span>&#123;</div><div class="line">        PostQueryInfo.DataBean data = datas.get(i);</div><div class="line">        view = inflater.inflate(R.layout.view_item_logistics, <span class="keyword">null</span>);</div><div class="line">        TextView tv_content= (TextView) view.findViewById(R.id.tv_conent);</div><div class="line">        TextView tv_date= (TextView) view.findViewById(R.id.tv_date);</div><div class="line">        tv_content.setText(data.getContext().replace(<span class="string">"["</span>,<span class="string">"【"</span>).replace(<span class="string">"]"</span>,<span class="string">"】"</span>));</div><div class="line">        tv_date.setText(data.getTime());</div><div class="line">        <span class="keyword">return</span> view;</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getCount</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> datas != <span class="keyword">null</span> ? datas.size() : <span class="number">0</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> Object <span class="title">getItem</span><span class="params">(<span class="keyword">int</span> i)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> datas.get(i);</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">long</span> <span class="title">getItemId</span><span class="params">(<span class="keyword">int</span> i)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> i;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>BaseView提供通用显示和隐藏加载框，如果有通用的方法都可以在写在这里面<br><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">BaseView</span> </span>&#123;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">showProgressDialog</span><span class="params">()</span></span>;    </div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">hideProgressDialog</span><span class="params">()</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>MainView接口继承提供MainActivity所有需要更新界面UI的方法<br><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">MainView</span> <span class="keyword">extends</span> <span class="title">BaseView</span></span>&#123;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">updateListUI</span><span class="params">(PostQueryInfo postQueryInfo)</span></span>;</div><div class="line">    <span class="function"><span class="keyword">void</span> <span class="title">errorToast</span><span class="params">(String message)</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>BaseActivity实现BaseVew所有接口方法<br><figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">BaseActivity</span> <span class="keyword">extends</span> <span class="title">Activity</span> <span class="keyword">implements</span> <span class="title">BaseView</span></span>&#123;</div><div class="line">    <span class="keyword">private</span> ProgressDialog progressDialog;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span></span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">        progressDialog = <span class="keyword">new</span> ProgressDialog(<span class="keyword">this</span>);</div><div class="line">        progressDialog.setMessage(<span class="string">"查询中..."</span>);</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">showProgressDialog</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">if</span>(progressDialog!=<span class="keyword">null</span>)&#123;</div><div class="line">            progressDialog.show();</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">hideProgressDialog</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="keyword">if</span>(progressDialog!=<span class="keyword">null</span>&amp;&amp;progressDialog.isShowing())&#123;</div><div class="line">            progressDialog.dismiss(); </div><div class="line">       &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>MainActivity继承BaseActivity实现MainView所有方法供PostPresenter调用，点击查询按钮通过实例化的PostPresenter，调用相关方法，后面的事情就全部交给PostPresenter去处理了，在activity销毁的时候调用postPresenter.detach()防止内存泄漏</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MainActivity</span> <span class="keyword">extends</span> <span class="title">BaseActivity</span> <span class="keyword">implements</span> <span class="title">View</span>.<span class="title">OnClickListener</span>, <span class="title">MainView</span> </span>&#123;</div><div class="line">    <span class="keyword">private</span> EditText post_name_et;</div><div class="line">    <span class="keyword">private</span> EditText post_id_et;</div><div class="line">    <span class="keyword">private</span> ListView post_list_lv;</div><div class="line">    <span class="keyword">private</span> Button post_search_bn;</div><div class="line">    <span class="keyword">private</span> PostPresenter postPresenter;</div><div class="line">    <span class="keyword">private</span> List&lt;PostQueryInfo.DataBean&gt; dataArray = <span class="keyword">new</span> ArrayList&lt;&gt;();</div><div class="line">    <span class="keyword">private</span> LogisticsAdapter logisticsAdapter;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span></span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">        setContentView(R.layout.activity_main);</div><div class="line">        initView();</div><div class="line">        initData();</div><div class="line">        initEvent();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initView</span><span class="params">()</span> </span>&#123;</div><div class="line">        post_name_et = (EditText) findViewById(R.id.post_name_et);</div><div class="line">        post_id_et = (EditText) findViewById(R.id.post_id_et);</div><div class="line">        post_list_lv = (ListView) findViewById(R.id.post_list_lv);</div><div class="line">        post_search_bn = (Button) findViewById(R.id.post_search_bn);</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initData</span><span class="params">()</span> </span>&#123;</div><div class="line">        logisticsAdapter = <span class="keyword">new</span> LogisticsAdapter(getApplicationContext(),dataArray);</div><div class="line">        postPresenter = <span class="keyword">new</span> PostPresenter(<span class="keyword">this</span>);</div><div class="line">        post_list_lv.setAdapter(logisticsAdapter);</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">initEvent</span><span class="params">()</span> </span>&#123;</div><div class="line">        post_search_bn.setOnClickListener(<span class="keyword">this</span>);</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onClick</span><span class="params">(View v)</span> </span>&#123;</div><div class="line">        <span class="keyword">switch</span>(v.getId())&#123;</div><div class="line">            <span class="keyword">case</span> R.id.post_search_bn:</div><div class="line">           postPresenter.requestHomeData(post_name_et.getText().toString(),post_id_et.getText().toString());</div><div class="line">                <span class="keyword">break</span>;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">updateListUI</span><span class="params">(PostQueryInfo postQueryInfo)</span> </span>&#123;</div><div class="line">        dataArray.clear();</div><div class="line">        dataArray.addAll(postQueryInfo.getData());</div><div class="line">        logisticsAdapter.notifyDataSetChanged();</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">errorToast</span><span class="params">(String message)</span> </span>&#123;</div><div class="line">        Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onDestroy</span><span class="params">()</span></span>&#123;</div><div class="line">        <span class="comment">//防止activity销毁,postPresenter对象还存在造成的内存泄漏</span></div><div class="line">        <span class="keyword">if</span>(postPresenter!=<span class="keyword">null</span>) postPresenter.detach();</div><div class="line">        <span class="keyword">super</span>.onDestroy();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="Presenter"><a href="#Presenter" class="headerlink" title="Presenter"></a>Presenter</h3><p>BasePresenter基类，提供了一个通用view对象，以及初始化和销毁view对象的方法</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">BasePresenter</span>&lt;<span class="title">T</span>&gt;</span>&#123;</div><div class="line">    <span class="keyword">public</span> T mView;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">attach</span><span class="params">(T view)</span></span>&#123;</div><div class="line">        <span class="keyword">this</span>.mView = view;</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">detach</span><span class="params">()</span></span>&#123;</div><div class="line">        mView = <span class="keyword">null</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>PostPresenter在构造函数中通过基类attach的方法初始化MainActivity提供的view对象，实例化PostSearchModelImpl对象，通过它提供的方法来请求网络数据，在成功和失败回调的函数里，可以做一些逻辑处理，通过view对象更新相应的UI</p>
<figure class="highlight java"><table><tr><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">PostPresenter</span> <span class="keyword">extends</span> <span class="title">BasePresenter</span>&lt;<span class="title">MainView</span>&gt;</span>&#123;</div><div class="line">    <span class="keyword">private</span> PostSearchModel postSearchModel;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">PostPresenter</span><span class="params">(MainView mainView)</span></span>&#123;</div><div class="line">        attach(mainView);</div><div class="line">        postSearchModel = <span class="keyword">new</span> PostSearchModelImpl();</div><div class="line">    &#125;</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">requestHomeData</span><span class="params">(String type,String postid)</span></span>&#123;</div><div class="line">        <span class="keyword">if</span>(postSearchModel == <span class="keyword">null</span>||mView == <span class="keyword">null</span>)</div><div class="line">            <span class="keyword">return</span>;</div><div class="line">        mView.showProgressDialog();</div><div class="line">        postSearchModel.requestPostSearch(type, postid, <span class="keyword">new</span> PostSearchModel.PostSearchCallback() &#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">requestPostSearchSuccess</span><span class="params">(PostQueryInfo postQueryInfo)</span> </span>&#123;</div><div class="line">                mView.hideProgressDialog();</div><div class="line">                <span class="keyword">if</span>(postQueryInfo!=<span class="keyword">null</span>&amp;&amp;<span class="string">"ok"</span>.equals(postQueryInfo.getMessage())) &#123;</div><div class="line">                    mView.updateListUI(postQueryInfo);</div><div class="line">                &#125;</div><div class="line">            &#125;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">requestPostSearchFail</span><span class="params">(String failStr)</span> </span>&#123;</div><div class="line">                mView.hideProgressDialog();</div><div class="line">                mView.errorToast(failStr);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>到这里全部代码基本都已编写完，代码的结构如下图：</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1460246-cadba6a41a9a8738.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="MVP结构图.png"></p>
<p>下面就运行程序看看效果吧，快递公司名和快递单号默认填写上去了，大家只需要点击查询，结果和下面图片一样就说明成功了</p>
<p><img src="http://upload-images.jianshu.io/upload_images/1460246-565de74e7178c7c3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="快递查询.png"></p>
<p>为什么没有直接提供一个可以运行的DEMO出来，原因很简单就是希望大家能够跟着一步一步学习，然后动手慢慢边敲代码边理解，最后敲完运行成功，应该就会更加理解MVP了。</p>
<h3 id="Tips"><a href="#Tips" class="headerlink" title="Tips"></a>Tips</h3><blockquote>
<p>上面MVC、MVP流程图制作的网站分享个大家：<br><a href="https://www.processon.com/i/53c777a70cf27d6e7f2c6fe5" target="_blank" rel="external">https://www.processon.com/</a><br>这个网站还可以做四维导图、UI原型图和UML图等等很多，有需要的可以去看看</p>
</blockquote>
<p><strong>成功源于不断的学习和积累</strong></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android/" rel="tag"># Android</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/09/05/2016-09-05-Retrofit/" rel="next" title="你必须了解的网络框架Retrofit2.0">
                <i class="fa fa-chevron-left"></i> 你必须了解的网络框架Retrofit2.0
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/09/23/2016-09-23-rxjava/" rel="prev" title="带大家进入RxJava的世界">
                带大家进入RxJava的世界 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/images/avatar.png"
               alt="Soaic" />
          <p class="site-author-name" itemprop="name">Soaic</p>
           
              <p class="site-description motion-element" itemprop="description">Welcome!</p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/archives/">
                <span class="site-state-item-count">10</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-categories">
              <a href="/categories/index.html">
                <span class="site-state-item-count">1</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/tags/index.html">
                <span class="site-state-item-count">3</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#配置环境"><span class="nav-number">1.</span> <span class="nav-text">配置环境</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#初始化配置Retrofit"><span class="nav-number">2.</span> <span class="nav-text">初始化配置Retrofit</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Model"><span class="nav-number">3.</span> <span class="nav-text">Model</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#View"><span class="nav-number">4.</span> <span class="nav-text">View</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Presenter"><span class="nav-number">5.</span> <span class="nav-text">Presenter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Tips"><span class="nav-number">6.</span> <span class="nav-text">Tips</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy;  2016 - 
  <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Soaic</span>
</div>

<script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="powered-by">
<i class="fa fa-user-md"></i><span id="busuanzi_container_site_uv">
  本站访客数:<span id="busuanzi_value_site_uv"></span>
</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.2"></script>



  


  




	





  





  






  





  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.4.js"></script>
  <script>AV.initialize("nSNhFcJCeysT9vRXgfNvtbQF-gzGzoHsz", "aegQNi8pntiOE4R2z0e5B84D");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  

  

  

  

</body>
</html>
